package com.test.dababy.dababy_07_loan_status;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Feed_dababybackActivity;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_00_utils.DababyConfigs;
import com.test.dababy.dababy_00_analytics.DababyUtil.DababyApRvUtil;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

public class RateNor_dababymalDialo_dababyg extends Dialog {

    private double dSuT=0.7093526470584854;
    private double hhk5=0.8344403617936133;
    private String l186="";
    private double m7JW=0.9202703863868691;
    private String p773="";

    public void nDnxX(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getL186())) setL186(String.valueOf(getdSuT())+getHhk5());
                else setP773(String.valueOf(getM7JW()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setdSuT(double dSuT) {
        this.dSuT = dSuT;
    }

    public void setHhk5(double hhk5) {
        this.hhk5 = hhk5;
    }

    public void setL186(String l186) {
        this.l186 = l186;
    }

    public void setM7JW(double m7JW) {
        this.m7JW = m7JW;
    }

    public void setP773(String p773) {
        this.p773 = p773;
    }

    public double getdSuT() {
        return dSuT;
    }

    public double getHhk5() {
        return hhk5;
    }

    public String getL186() {
        return l186;
    }

    public double getM7JW() {
        return m7JW;
    }

    public String getP773() {
        return p773;
    }

    private final String url_dababy = "https://play.google.com/store/apps/details?id=" + DababyConfigs.PACK_dababyAGE_NAME;
    private int score_dababy = 0;
    private static boolean isShow_dababy = false;
    private final ImageButton[] starIbs_dababy = new ImageButton[5];

    private final FragmentActivity activity_dababy;

    public RateNor_dababymalDialo_dababyg(FragmentActivity activity){
        super(activity);
        this.activity_dababy = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dababy_rate_normal);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);


        findViewById(R.id.ddrn_ib_close).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_NORMAL","CLOSE_CLICK",String.valueOf(score_dababy));
            dismiss();
        });

        TextView textTv = findViewById(R.id.ddrn_tv_text);

        String content = Rate_dababyNormalW_dababyindowConfig.getInstance().getContent_dababy();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs_dababy[0] = findViewById(R.id.ddrn_ib_star1);
        starIbs_dababy[1] = findViewById(R.id.ddrn_ib_star2);
        starIbs_dababy[2] = findViewById(R.id.ddrn_ib_star3);
        starIbs_dababy[3] = findViewById(R.id.ddrn_ib_star4);
        starIbs_dababy[4] = findViewById(R.id.ddrn_ib_star5);

        findViewById(R.id.ddrn_btn_rate).setOnClickListener(v -> {

            OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_NORMAL","RATE_CLICK",String.valueOf(score_dababy));

            if (score_dababy ==0) {
                Toast.makeText(activity_dababy, activity_dababy.getString(R.string.please_rate),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score_dababy >=4){
                if (App_dababyShow_dababyCustomize.getInstance_dababy().showSysReview_dababy()){
                    DababyApRvUtil.INSTANCE.launchRvFlow_dababy((AppCompatActivity) activity_dababy, aBoolean -> {
                        if (!aBoolean) activity_dababy.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_dababy)));
                        dismiss();
                        return null;
                    });
                }else activity_dababy.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_dababy)));
            }else activity_dababy.startActivity(new Intent(activity_dababy, Feed_dababybackActivity.class));
            dismiss();
        });

        for (int i = 0; i< starIbs_dababy.length; i++){
            ImageButton scoreIb = starIbs_dababy[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore_dababy(fScore));
        }
    }


    private void setScore_dababy(int score_dababy){

        if (score_dababy >5) {
            nDnxX();
            score_dababy =5;
        }
        else if (score_dababy <1) score_dababy =1;

        this.score_dababy = score_dababy;

        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_NORMAL","STAR_CLICK",String.valueOf(score_dababy));

        for (int i = 0; i< starIbs_dababy.length; i++){
            ImageButton scoreIb = starIbs_dababy[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i< score_dababy ? R.drawable.icon_rate_star_light :R.drawable.icon_rate_star_gray);
        }
    }

    @Override
    public void show() {
        if (isShow_dababy) return;
        isShow_dababy = true;
        super.show();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_NORMAL","START","");
        DababyUtil.addNormaldialogCount_dababy();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_NORMAL","END","");
        isShow_dababy = false;
    }

}
